import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shiva   2023-01-24 0:20
 */
public class MainRunner {

    public static void main(String[] args) {
        MainRunner runner = new MainRunner();
//        runner.wordReader();//文件读取
        runner.wordTemplateWrite();//模板写入
    }

    /**
     * word 文件读取
     */
    public void wordReader() {
        File file = new File("C:\\Users\\shiva\\Desktop\\1.docx");
        try {
            FileInputStream fis = new FileInputStream(file);
            XWPFDocument document = new XWPFDocument(fis);
            XWPFWordExtractor extractor = new XWPFWordExtractor(document);
            String text = extractor.getText();
            System.out.println(text);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * word 模板读取写入
     */
    public void wordTemplateWrite() {
        Map<String, Object> map = new HashMap<>();
        map.put("department", "Easypoi");
        map.put("person", "JueYue");
        try {
            XWPFDocument doc = WordExportUtil.exportWord07("C:\\Users\\shiva\\Desktop\\1.docx", map);
            FileOutputStream fos = new FileOutputStream("C:\\Users\\shiva\\Desktop\\2.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
